﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

 namespace Small.Util.Reflection

{
    public class AnonymousProperties
    {


        /// <summary>
        /// 获取到匿名类的声明名称和值
        /// </summary>
        /// <param name="anonymous"></param>
        /// <returns></returns>
        public static  IEnumerable<AnonymousInfo> GetAnonymousInfomation(object anonymous) 
        {
           
            if (anonymous != null)
            {
                var list = anonymous.GetType().GetProperties().Select(p => new AnonymousInfo { Name = p.Name, Value = p.GetValue(anonymous) });
                if (list != null) 
                {
                    return list;
                }
            }

            return null;
        }

        /// <summary>
        /// 获取到单个值得名称
        /// </summary>
        /// <param name="anonymous"></param>
        /// <returns></returns>
        public static string GetPropertyName<T>(Func<String,bool> pointField)
        {
            T t = default(T);
            var result = t.GetType().GetProperties().Select(p => p
                .Name);
            if (result != null)
            {
                var result1 = result.Where(pointField).FirstOrDefault();
                if (result1 != null) 
                {
                    return result1;
                }
            }

          return null;
        }

        
    }
}